<?php
/*
+---------------------------------------------------------------+
| for e107Shop 1.2.X by http://e107.ir Persian e107 Support     |
| Creators : Persian e107 support and Webdoktor (NL)            |
| Bugfixed and streamlined by Bieleke (e107shop.com)            |
+---------------------------------------------------------------+
*/
$cart_menu_Debug = false;
require('e107shop_folder.php');
require(e_PLUGIN.E107SHOP.'/db_names.php');
require_once(e_PLUGIN.E107SHOP.'/library/session_handler.php');
require_once(e_PLUGIN.E107SHOP.'/library/products_class.php');
require_once(e_PLUGIN.E107SHOP.'/library/settings_class.php');
require_once(e_PLUGIN.E107SHOP.'/library/calculate_class.php');

$cart_id = e107shop_get_cartid();
global $sql, $tp;
// Count the products in cart
$sql -> db_Select("e107shop_cart", "sum(`quantity`)", "cart_ID = '".$cart_id."';", "default", $cart_menu_Debug);
$row = $sql->db_Fetch();
$amount = $row[0];

$sql->db_Select("e107shop_settings", "*", "", "no-where", $cart_menu_Debug);
while ($row=$sql->db_Fetch()) {
    $shop_settings = $row;
}

if (ADMIN && $cart_menu_Debug) {
    $errormsg = print_a($shop_settings, true);
    error_log($errormsg);
}

if($sql->db_Select("e107shop_cart", "*", "cart_ID='".$cart_id."' order by cart_time", "default",$cart_menu_Debug)) {
    $price = 0;
    $postage_price = 0;
    $total_price = 0;
    $quantity = 0;
    while($row = $sql -> db_Fetch()){
        $line_product_price = e107shop_get_product_field($row["productID"], "special_price");
        if (floatval($line_product_price) == 0) 
            $line_product_price = e107shop_get_product_field($row["productID"], "product_price");
        // Now check if there are option related tot he product
        $options = explode("|", $row["option"]);
        if(is_array($options)) {
            foreach ($options as $value) {
                $line_option_price = e107shop_get_option_field($value, "optval");
                $line_product_price += $line_option_price;
            }
        }
        $product_price += $line_product_price * $row['quantity'];

        //
        // Calculate the postage price
        //
        $postage_price += e107shop_get_postage_price($row["productID"], $row['quantity']);
    }
    // calculate and add the tax
    if ($shop_settings['tax_inc'] == 1) $product_price = roundfix(e107shop_calculate_excl($product_price, $shop_settings['tax_rate']), 2);        
    $postage_price = roundfix($postage_price, 2);
    $subtotal = roundfix($product_price + $postage_price, 2);
    $tax = roundfix(e107shop_calculate_tax($subtotal, $shop_settings['tax_rate']), 2);
    $total = $shop_settings["currency_symbol"].e107shop_get_numformat($subtotal + $tax);
}

// if the customer has his own cart picture display that, else display the default
if ($amount > 0) {
    if (file_exists(e_PLUGIN.E107SHOP."/images/cartfull_custom.png")) $image = "cartfull_custom.png";
    else $image = "cartfull.png";
} else {
    if (file_exists(e_PLUGIN.E107SHOP."/images/cartempy_custom.png")) $image = "cartempty_custom.png";
    else $image = "cartempty.png";
}
$text = "
<div style='width: 100%;'>
<table>
    <tr>
        <td style='text-align: center;'>
            <a href='".e_PLUGIN.E107SHOP."/cart.php'><img border='0' src='".e_PLUGIN.E107SHOP."/images/".$image."' alt='Shopping Cart' /></a>
        </td>
    </tr>
</table>";
if ($amount > 0) $text .= "
<table border='0' cellpadding='5' cellspacing='10'>
    <tr>
        <td style='text-align: center; font-size: 12px;'>
            ".lan_global_quantity." : ".$amount."
        </td>
    </tr>
    <tr>
        <td style='text-align: center; font-size: 12px;'>
            <div style='padding-bottom: 10px;'>".lan_global_price." : ".$total."
        </td>
    </tr>
    <tr>
        <td style='text-align: center; font-size: 12px;'>
            <input class='button' type='button' value='".lan_global_view_cart."' onclick='window.location.href=\"".e_PLUGIN.E107SHOP."/cart.php\"' />
        </td>
    </tr>
    <tr>
        <td style='text-align: center; font-size: 12px;'>
            <input class='button' type='button' value='".lan_global_checkout."' onclick='window.location.href=\"".e_PLUGIN.E107SHOP."/checkout.php\"' />
        </td>
    </tr>
</table>";
$text .= "</center>";


$text .= "</div>";
  
// You can set all the translated text used here in the language file around line 50
$caption = lan_global_shopping_cart;
$ns -> tablerender($caption, $text, 'rand');
